package com.roadjava.javaweb.student.servlet;

import com.roadjava.javaweb.student.beans.entity.ScoreDO;
import com.roadjava.javaweb.student.beans.req.StudentRequest;
import com.roadjava.javaweb.student.beans.res.ResultDTO;
import com.roadjava.javaweb.student.beans.res.TableResult;
import com.roadjava.javaweb.student.beans.vo.ScoreVO;
import com.roadjava.javaweb.student.beans.vo.StudentVO;
import com.roadjava.javaweb.student.service.ScoreService;
import com.roadjava.javaweb.student.service.impl.ScoreServiceImpl;
import com.roadjava.javaweb.student.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "scoreServlet",urlPatterns = "/scoreServlet")
public class ScoreServlet extends HttpServlet {
    private ScoreService scoreService = new ScoreServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        if ("toScoreManage".equals(type)) {
            request.getRequestDispatcher("/WEB-INF/score/scoreManage.jsp").forward(request,response);
        }else if ("add".equals(type)) {
            String studentId = request.getParameter("studentId");
            String cnScore = request.getParameter("cnScore");
            String enScore = request.getParameter("enScore");
            String mathScore = request.getParameter("mathScore");
            ScoreDO scoreDO = new ScoreDO();
            scoreDO.setStudentId(Long.parseLong(studentId));
            scoreDO.setCnScore(Double.valueOf(cnScore));
            scoreDO.setEnScore(Double.valueOf(enScore));
            scoreDO.setMathScore(Double.valueOf(mathScore));
            boolean b = scoreService.addScore(scoreDO);
            if (b) {
                ResponseUtil.respAppJson(response,ResultDTO.buildSuccess("添加成绩成功"));
            } else {
                ResponseUtil.respAppJson(response,ResultDTO.buildFailure("添加成绩失败"));
            }
        }else if ("retrieveScores".equals(type)) {
            String studentName = request.getParameter("studentName");
            String pageNowStr = request.getParameter("pageNow");//当前第几页
            StudentRequest studentRequest = new StudentRequest();
            studentRequest.setPageNow(Integer.parseInt(pageNowStr));
            studentRequest.setStudentName(studentName);
            ResultDTO<TableResult<ScoreVO>> resultDTO = scoreService.queryScoreByPage(studentRequest);
            ResponseUtil.respAppJson(response,resultDTO);
        }else if ("getScoreById".equals(type)) {
            String scoreId = request.getParameter("id");
            ResultDTO<ScoreVO> resultDTO = scoreService.getScoreById(Long.parseLong(scoreId));
            ResponseUtil.respAppJson(response,resultDTO);
        }else if ("update".equals(type)) {
            String scoreId = request.getParameter("id");
            String studentId = request.getParameter("studentId");
            String cnScore = request.getParameter("cnScore");
            String enScore = request.getParameter("enScore");
            String mathScore = request.getParameter("mathScore");
            ScoreDO scoreDO = new ScoreDO();
            scoreDO.setId(Long.parseLong(scoreId));
            scoreDO.setStudentId(Long.parseLong(studentId));
            scoreDO.setCnScore(Double.valueOf(cnScore));
            scoreDO.setEnScore(Double.valueOf(enScore));
            scoreDO.setMathScore(Double.valueOf(mathScore));
            boolean b = scoreService.updateScore(scoreDO);
            if (b) {
                ResponseUtil.respAppJson(response,ResultDTO.buildSuccess("修改成绩成功"));
            } else {
                ResponseUtil.respAppJson(response,ResultDTO.buildFailure("修改成绩失败"));
            }
        }else if ("delete".equals(type)) {
            String scoreId = request.getParameter("id");
            boolean b = scoreService.deleteScoreById(Long.parseLong(scoreId));
            if (b) {
                ResponseUtil.respAppJson(response,ResultDTO.buildSuccess("修改成绩成功"));
            } else {
                ResponseUtil.respAppJson(response,ResultDTO.buildFailure("修改成绩失败"));
            }
        }
    }
}
